#include <Windows.h>
#include <stdio.h>

LRESULT CALLBACK winProc(
	HWND hwnd,
	UINT uMsg,
	WPARAM wParam,
	LPARAM lParam
);

int WINAPI WinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpCmdLine,
	int nCmdShow
){
	MessageBox(NULL, TEXT("WM_CREATE"), TEXT("Information"), 1);
	WNDCLASS winClass;
	winClass.cbClsExtra = 0;
	winClass.cbWndExtra = 0;
	winClass.hbrBackground = (HBRUSH)GetStockObject(DEFAULT_GUI_FONT);//(HBRUSH)COLOR_ACTIVEBORDER;
	winClass.hCursor = LoadCursor(NULL, IDC_WAIT);
	winClass.hIcon = LoadIcon(NULL, IDI_ERROR);
	winClass.hInstance = hInstance;
	winClass.lpfnWndProc = winProc;
	winClass.lpszClassName = TEXT("Class Name");
	winClass.lpszMenuName = TEXT("Menu Name");
	winClass.style = CS_HREDRAW | CS_VREDRAW;

	RegisterClass(&winClass);

	HWND hwnd = CreateWindow(winClass.lpszClassName, TEXT("Test Window"), WS_OVERLAPPEDWINDOW, 100, 100, 300, 200, NULL, NULL, hInstance, NULL);
	ShowWindow(hwnd, SW_SHOWNORMAL);
	UpdateWindow(hwnd);

	MSG msg;
	while(GetMessage(&msg, NULL, 0, 0)){
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return 0;
}

LRESULT CALLBACK winProc(
	HWND hwnd,
	UINT uMsg,
	WPARAM wParam,
	LPARAM lParam
){
	switch(uMsg){
	case WM_CREATE:
		// HWND hwndButton = CreateWindow(TEXT("button"), TEXT("Button"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 80, 10, 100, 50, hwnd, NULL, NULL, NULL);
		MessageBox(hwnd, TEXT("WM_CREATE"), TEXT("Information"), MB_RETRYCANCEL);
		break;
	case WM_CHAR:{
		char* chPtr = (char*)malloc(sizeof(char) * 1);
		sprintf(chPtr, "Char is %c", wParam);
		wchar_t* wch = (wchar_t*)malloc(sizeof(wchar_t) * 1);
		mbstowcs_s(0, wch, strlen(chPtr) + 1, chPtr, _TRUNCATE);
		
		MessageBox(hwnd, wch, TEXT("Information"), MB_OK);
		break;
		}
	case WM_LBUTTONDOWN:
		HDC hdc;
		hdc = GetDC(hwnd);
		TextOut(hdc, 0, 50, TEXT("A text out message."), strlen("A text out message."));
		ReleaseDC(hwnd, hdc);
		break;
	case WM_PAINT:
		HDC hdc_paint;
		PAINTSTRUCT paint;
		hdc_paint = BeginPaint(hwnd, &paint);
		TextOut(hdc_paint, 0, 100, TEXT("A text out message. -- WM_PAINT"), strlen("A text out message. -- WM_PAINT"));
		EndPaint(hwnd, &paint);
		break;
	case WM_CLOSE:
		if(IDYES == MessageBox(hwnd, TEXT("Exit the application"), TEXT("Warning"), MB_YESNO)){
			DestroyWindow(hwnd);
		}
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd, uMsg, wParam,lParam);
	}
	
	return 0;
}